using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XFGameFramework;

public class TimeController : Controller
{
    private GameSaveController gameSaveController => Module.LoadController<GameSaveController>();

    private GameController gameController => Module.LoadController<GameController>();
    private TimeModel _timeModel;

    /// <summary>
    /// 重新引用,防止引用丢失
    /// </summary
    public void Reload()
    {
        _timeModel = gameSaveController.GetTimeModel(0);
    }

    #region 属性
    // 总分钟数访问器（最高优先级）
    public int TotalMinutes
    {
        get => _timeModel.days * 1440 + _timeModel.hours * 60 + _timeModel.minutes;
        set
        {
            // 处理负数（根据需求可选）
            value = Mathf.Max(value, 0);

            _timeModel.days = value / 1440;
            int remaining = value % 1440;
            _timeModel.hours = remaining / 60;
            _timeModel.minutes = remaining % 60;
        }
    }

    // 总小时数访问器（可选）
    public float TotalHours
    {
        get => TotalMinutes / 60f;
        set => TotalMinutes = Mathf.RoundToInt(value * 60);
    }

    // 带进位的小时属性
    public int Hours
    {
        get => _timeModel.hours;
        set
        {
            int carryDays = Mathf.FloorToInt(value / 24f);
            _timeModel.days += carryDays;
            _timeModel.hours = value % 24;

            // 处理负数（如允许负时间则需要调整）
            if (_timeModel.hours < 0)
            {
                _timeModel.days -= 1;
                _timeModel.hours += 24;
            }
        }
    }

    // 带进位的分钟属性
    public int Minutes
    {
        get => _timeModel.minutes;
        set
        {
            int carryHours = Mathf.FloorToInt(value / 60f);
            Hours += carryHours; // 复用已有进位逻辑
            _timeModel.minutes = value % 60;

            // 处理负数
            if (_timeModel.minutes < 0)
            {
                Hours -= 1;
                _timeModel.minutes += 60;
            }
        }
    }

    // 天数独立属性（通常不需要进位）
    public int Days
    {
        get => _timeModel.days;
        set => _timeModel.days = Mathf.Max(value, 0);
    }
    public int RemainingTime
    {
        get => _timeModel.remainingTime;
        set => _timeModel.remainingTime = value;
    }
    public Period CurrentPeriod
    {
        get => _timeModel.currentPeriod;
    }
    #endregion
    #region 方法
    public void Set(TimeModel other)
    {
        if (other == null) return;
        _timeModel.days = other.days;
        _timeModel.hours = other.hours;
        _timeModel.minutes = other.minutes;
        _timeModel.currentPeriod = other.currentPeriod;
        _timeModel.remainingTime = other.remainingTime;
    }

    #endregion
    #region 重载方法
    public override void OnInit()
    {
        base.OnInit();
        _timeModel = gameSaveController.GetTimeModel(0);
       
    }
    #endregion
}

